package com.openlogic.common.logic.log.strategy;

import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.AppBeanLoadEndEvent;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.util.MultiMap;

import com.alibaba.fastjson2.JSON;
import com.openlogic.api.logic.entity.RuleExecLog;
import com.openlogic.common.logic.annotation.RuleLog;
import com.openlogic.common.logic.constants.RuleTypeConstants;
import com.openlogic.common.logic.log.factory.RecordLogFactory;

import cn.hutool.core.util.ObjectUtil;

/**
* @ClassName: DecisionTreeLogService
* @Description: 决策流日志处理实现
* @author weiyongpeng
* @date 2024年10月9日 上午9:22:24
 */
@Component
public abstract class DecisionFlowLogService extends RecordDecisionLogStrategy {

	@Override
	protected void getControllerMethodDescription(Context joinPoint, RuleLog ruleLog,
			RuleExecLog operateLog, Object jsonResult) throws Exception {
		// 获取参数的信息，传入到数据库中。
    	setRequestValue(joinPoint, operateLog);
    	// 出参
//    	String jsonResultStr = JSON.toJSONString(JSONObject.parseObject(JSON.toJSONString(jsonResult)).get(Result.MSG_TAG));
//    	operateLog.setRepParam(StringUtils.substring(jsonResultStr, 0, 4000));
//    	net.sf.json.JSONObject fromObject = net.sf.json.JSONObject.fromObject(net.sf.json.JSONObject.fromObject(jsonResult).get(Result.MSG_TAG));
//    	if (Optional.ofNullable(fromObject.get("everyNodeList")).isPresent()) {
//    		JSONArray nodeArray = JSONArray.fromObject(fromObject.get("everyNodeList"));
//			Object errorJson = nodeArray.stream().filter(node -> net.sf.json.JSONObject.fromObject(node).containsKey("errorMsg")).findFirst().orElse(null);
//    		if (errorJson != null) {
//				net.sf.json.JSONObject errorNode = net.sf.json.JSONObject.fromObject(errorJson);
//				operateLog.setStatus(ExecConstants.ERROR);
//				operateLog.setErrorMsg(StringUtils.substring(errorNode.get("errorMsg").toString(), 0, 4000));
//			}
//		}
	}

	@Override
	protected String argsArrayToString(MultiMap<String> paramMap, RuleExecLog operLog) {
		String params = "";
		if (paramMap != null) {
			paramMap.forEach(x -> {
				Object[] values = x.getValues().toArray();
				for (int i = 0; i < values.length; i++) {
					Object o = values[i];
					if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 0) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 入参
							operLog.setReqParam(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 1) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 决策树json字符串
							operLog.setRuleInfo(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 2) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 规则链id
							operLog.setRuleId(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 3) {
						try {
							Object jsonObj = JSON.toJSON(o);
							operLog.setRuleName(jsonObj.toString());
						} catch (Exception e) {
						}
					} else if (ObjectUtil.isNotNull(o) && !super.isFilterObject(o) && i == 4) {
						try {
							Object jsonObj = JSON.toJSON(o);
							// 规则链原始json
							operLog.setRuleRawValue(jsonObj.toString());
						} catch (Exception e) {
						}
					}
				}
			});
		}
	    return params.trim();
	}
	

	@Override
	public void onEvent(AppBeanLoadEndEvent event) throws Throwable {
		RecordLogFactory.register(RuleTypeConstants.ruleflow.getCode(), this);
	}

}
